﻿using EmbeddedSensorCloud.Command;
using EmbeddedSensorCloud.Core;
using EmbeddedSensorCloud.Core.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace EmbeddedSensorCloud.Console.Commands
{
    public class ServerCommand : CommandBase
    {
        private readonly ServerCore worker;

        public ServerCommand(ServerCore worker)
        {
            if (worker == null)
            {
                throw new ArgumentException("ServerCore cannot be null");
            }

            this.worker = worker;
        }

        protected override void execute(string[] param)
        {
            if (param == null || param.Length == 0)
            {
                throw new ArgumentException("Arguments cannot be null or empty");
            }

            if (param[0] == "on")
            {
                int port = 8080;
                IPAddress ip = IPAddress.Any;

                if (param.Length >= 2)
                {
                    if (!IPAddress.TryParse(param[1], out ip))
                    {
                        ip = IPAddress.Any;
                        base.RaiseLogged("Wrong ip address. Using default ip: " + ip.ToString());
                    }
                }

                if (param.Length >= 3)
                {
                    if (!Int32.TryParse(param[2], out port))
                    {
                        port = 8080;
                        base.RaiseLogged("Wrong port. Using default port: " + port);
                    }
                }

                try
                {
                    this.worker.Start(ip, port);
                    base.RaiseLogged(string.Format("Server started listening on {0}:{1}.", this.worker.IP, this.worker.Port));
                }
                catch (ServerException ex)
                {
                    base.RaiseLogged(ex.ToString());
                }
            }
            else if (param[0] == "off")
            {
                try
                {
                    this.worker.Stop();
                    base.RaiseLogged(string.Format("Server stopped listening on {0}:{1}.", this.worker.IP, this.worker.Port));
                }
                catch (ServerNotListeningException ex)
                {
                    base.RaiseLogged(ex.ToString());
                }
                catch (ServerException ex)
                {
                    base.RaiseLogged(ex.ToString());
                }
            }
        }

        public override string Description
        {
            get { return "Starts or stops the webserver"; }
        }

        public override string Name
        {
            get { return "server"; }
        }

        public override string Syntax
        {
            get { return "server [on {<ip>} {<port>}] [off]"; }
        }

        public override string RequiredArgumentRegex
        {
            get { return "on( (([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]).){3}([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])){0,1}( [0-9]{1,5}){0,1}|off"; }
        }
    }
}